import { action, observable, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';
import * as odocSignApprovalApi from '../../apis/odocSignApprovalApi';

const getLabel = WeaLocaleProvider.getLabel;

export default class odocSignApprovalStore {
  // 顶部操作按钮
  @observable leftKeys = [{
    key: 'wsZoomOutBtn',
    value: getLabel('22208', '放大'),
    icon: 'icon-coms02-enlarge',
  }, { key: 'wsZoomInBtn', value: getLabel('22209', '缩小'), icon: 'icon-coms02-narrow' },
  { key: 'writeBtn', value: getLabel('504594', '签批'), icon: 'icon-coms02-Sign-off' }, {
    key: 'wsSaveBtn',
    value: getLabel('30986', '保存'),
    icon: 'icon-coms02-Save-as',
  }];
  @observable RrightKeys = [{
    key: 'validBtn',
    value: getLabel('521869', '批注清单'),
    icon: 'icon-coms02-Signature-verification',
  }, { key: 'thumbBtn', value: getLabel('19064', '缩略图'), icon: 'icon-coms02-thumbnail' }, {
    key: 'signFile',
    value: getLabel('130182', '签批文件'),
    icon: 'icon-coms-content-o',
  }, { key: 'viewHistory', value: getLabel('502492', '查看历史版本'), icon: 'icon-coms-daiban' }];

  @observable requestid = -1;
  @observable workflowid = -1;
  @observable nodeid = -1;

  @observable ctx = null;// 好签文本对象
  @observable cssPath = '';
  @observable jsPath = '';
  @observable userid = -1;
  @observable lastname = '';
  @observable sequenceId = '';
  @observable sealName = '';
  @observable sealid = -1;
  @observable token = '';

  @observable hasLoad = false;
  @observable scale = 0;


  @observable isShowHistroy = false;// 是否显示历史版本
  @observable isView = false;// 只查看不可签批
  @observable isSeal = false;// 可签批
  @observable isOpen = false;// 默认打开
  @observable isMust = false;// 是否必须签批
  @observable sealDocs = [];// 签批文档
  @observable histroyDocs = [];// 历史文档
  @observable isShowMore = false;

  @observable isviewHistory = false;
  @observable historyImagefiles = []
  @observable historyImagefileid = -1;
  @observable imagefileid = '';
  @observable previewUrl = '';
  @observable historyVersionName = '';

  @observable HistorySelectedKeys = [];
  @observable HistoryQianSelectedKeys = [];
  @observable QianSelectedKeys = [];
  @observable isSave = true;
  @observable showThumb = false;
  @observable qpSelected = false;
  @observable defaultSign = true;
  @observable device = '';
  @observable defaultWriteColor = ''// 默认签批颜色
  @observable defaultWriteSize = 3;// 默认签批笔迹粗细
  @observable isUsePDFViewer = false;
  @observable loading = false;
  @observable f_weaver_belongto_userid = '';
  @observable f_weaver_belongto_usertype = '';
  @observable isClickSave = false;
  @observable isDownload = false;
  @observable downloadBtnName = "";

  @action
  setProperty = (map) => {
    for (let i in map) {
      this[i] = map[i];
    }
  }

  @action
  setQpSelected = (v) => {
    this.qpSelected = v;
  }

  @action
  convertFile = () => {
    this.loading = true;
    let params = {
      fileid: this.imagefileid,
    };
    odocSignApprovalApi.convertFile(params).then((data) => {
      if (data.data && data.data.length > 0) {
        this.previewUrl = data.data[0].path;
        this.loading = false;
      }
    });
  }

  @action
  updateHeaderPosition = () => {
    let middleWidth = 0;
    for (let i = 0; i < jQuery('.haoqian-middle button').length; i++) {
      middleWidth += jQuery(`.haoqian-middle button:eq(${i})`).width();
    }
    let rightWidth = jQuery('.haoqian-right').width();
    let winWidth = jQuery(window).width();
    if (winWidth / 2 < (rightWidth + middleWidth / 2 + 30)) {
      jQuery('.haoqian-middle').css({
        'padding-right': `${rightWidth + 10}px`,
        'text-align': 'right',
      });
      jQuery('.wea-haoqian-header').css({ 'min-width': `${middleWidth + rightWidth + 20}px` });
    } else {
      jQuery('.haoqian-middle').css({
        'padding-right': 0,
        'text-align': 'center',
      });
    }
  }

  @action
  getSignApprovalDatas = () => {
    this.loading = true;
    let _that = this;
    let params = {
      workflowid: this.workflowid,
      requestid: this.requestid,
      nodeid: this.nodeid,
      userid: this.userid,
      f_weaver_belongto_userid: this.f_weaver_belongto_userid,
      f_weaver_belongto_usertype: this.f_weaver_belongto_usertype,
    };
    return odocSignApprovalApi.getHqSealInfo(params).then((data) => {
      if (data.api_status == 0) {
        message.error(data.msg || '获取签批数据失败');
      } else if (data.sealDocs && data.sealDocs.length > 0) {
        this.setProperty(data);
        let firstDatas = data.sealDocs[0];
        this.sequenceId = firstDatas.sequenceId;
        this.sealName = firstDatas.imagefilename;
        this.sealid = firstDatas.id;
        this.QianSelectedKeys = [firstDatas.sequenceId];
        this.historyImagefileid = firstDatas.encodeimagefileid;
        this.imagefileid = firstDatas.imagefileid;
        if (data.device == 'EC') {
          this.leftKeys = [];
          // this.RrightKeys = [];

          this.hasLoad = true;
          if (this.isUsePDFViewer == true) {
            this.convertFile(firstDatas.imagefileid);
          } else {
            this.loading = false;
          }
          return false;
        }
        this.loading = false;
        let timer = setInterval(() => {
          if (jQuery('.haoqian-right').length > 0 && jQuery('.haoqian-right').width() > 0) {
            clearInterval(timer);
            _that.updateHeaderPosition();
          }
        }, 750);
        return true;
      } else {
        message.error(data.msg || '获取签批数据失败');
      }
    });
  }

  @action
  doSubmitValidate = (callback) => {
    let params = {
      requestid: this.requestid,
      nodeid: this.nodeid,
      f_weaver_belongto_userid: this.f_weaver_belongto_userid,
      f_weaver_belongto_usertype: this.f_weaver_belongto_usertype,
    };
    odocSignApprovalApi.hasSeal(params).then((data) => {
      if (data.api_status && data.api_status == 1) {
        if (data.hasSeal && data.hasSeal == true) {
          callback();
        } else {
          message.error(getLabel('521680', '签批未完成'));
        }
      } else {
        message.error(data.msg);
      }
    });
  }

  @action
  saveSignApprovalDatas = (buildNo) => {
    let params = {
      workflowid: this.workflowid,
      requestid: this.requestid,
      nodeid: this.nodeid,
      sequenceId: this.sequenceId,
      buildNo,
      imagefilename: this.sealName,
      sealid: this.sealid,
      userid: this.userid,
      f_weaver_belongto_userid: this.f_weaver_belongto_userid,
      f_weaver_belongto_usertype: this.f_weaver_belongto_usertype,
    };
    odocSignApprovalApi.saveH5SealDoc(params).then((data) => {
      this.isSave = false;
      this.sealDocs = toJS(this.sealDocs).map((item) => {
        if (item.sequenceId == this.sequenceId) {
          item.buildNo = buildNo;
          item.encodeimagefileid = data.encodeimagefileid;
          item.imagefileid = data.imagefileid;
          item.buildNo = buildNo;
        }
        return item;
      });
      this.historyImagefileid = data.encodeimagefileid;
      this.imagefileid = data.imagefileid;
    });
  }

  @action('签批签入签出')
  doCheckSignApprovalFile = (type) => {
    let params = {
      checktype: type,
      requestid: this.requestid,
      userid: this.userid,
      f_weaver_belongto_userid: this.f_weaver_belongto_userid,
      f_weaver_belongto_usertype: this.f_weaver_belongto_usertype,
    };
    odocSignApprovalApi.checkSeal(params).then(() => { });
  }


  resourceFiles = () => [
    this.cssPath,
    this.jsPath,
  ]

  setCtx(ctx) {
    this.ctx = ctx;
  }

  @action
  setSequenceId(sequenceId) {
    this.sequenceId = sequenceId;
  }
}
